package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.UserInfoManager;
import com.common.model.StaffInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/user")
public class UserInfoController extends BaseController{

	 private static String modelUrl="/management/user/";
	 
    @Autowired
	private UserInfoManager userInfoManager;

	@RequestMapping("/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("userInfoModel") StaffInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = userInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	

	/**
	 * 查询分页，提供给机构树使用
	 * @param vo
	 * @param param
	 * @param orgCode
	 * @return
	 */
	@RequestMapping("/{orgCode}/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("userInfoModel") StaffInfoModel param,@PathVariable("orgCode") String orgCode) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		param.setOrgCode(orgCode);
		List<DataMap> dataList = userInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("userInfoModel") StaffInfoModel userInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",userInfoModel);
		return modelView;
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"edit");
		IData dataMap = new DataMap();
		dataMap.put("staffNum", intID);
		StaffInfoModel userInfoModel= (StaffInfoModel)userInfoManager.getObjectInfo(dataMap);
		modelView.addObject("vo",userInfoModel);
		return modelView;
	}
	
	
	/**
	 * 功能：执行插入操作
	 * @param userInfoModel
	 * @return
	 */
	@RequestMapping("insert")
	public ModelAndView insert(HttpServletRequest request,@ModelAttribute("userInfoModel") StaffInfoModel userInfoModel) {
		
		String porgName = request.getParameter("orgLookup.porgName");
		String porgCode = request.getParameter("orgLookup.porgCode");
		String message="";
		userInfoModel.setOrgCode(porgCode);
		userInfoModel.setOrgName(porgName);
		
		int opNum = userInfoManager.insertObjectAndGetID(userInfoModel);
		if(opNum>0){
			message = "添加成功";
		}else{
			message = "添加失败";
		}
		ModelAndView modelView = ajaxDoneSuccess(message);
		modelView.addObject("param", userInfoModel);
		return modelView;
	}
	
	
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,@ModelAttribute("userInfoModel") StaffInfoModel userInfoModel) {
		String porgName = request.getParameter("orgLookup.porgName");
		String porgCode = request.getParameter("orgLookup.porgCode");
		String message="";
		userInfoModel.setOrgCode(porgCode);
		userInfoModel.setOrgName(porgName);
		int opNum =userInfoManager.updateObject(userInfoModel);
		if(opNum>0){
			message = "更新成功";
		}else{
			message = "更新失败";
		}
		ModelAndView modelView= ajaxDoneSuccess(message);
		modelView.addObject("param", userInfoModel);
		return modelView;
	}
	
	
	@RequestMapping("/{ids}/delete")
	public ModelAndView delete(@PathVariable("ids") String ids) {
		String message="";
		int opNum =userInfoManager.deleteObject(ids);
		if(opNum>0){
			message = "删除成功";
		}else{
			message = "删除失败";
		}
		ModelAndView modelView= ajaxDoneSuccess(message);
		return modelView;
	}
}